package com.yunzhi.net.api;


import com.cloudknow.bean.RoleLoginBean;
import com.cloudknow.bean.UserInfoBean;
import com.google.gson.Gson;
import com.yunzhi.net.entity.BaseResponse;
import com.yunzhi.net.retrofit.RetrofitFactory;
import com.yunzhi.net.scheduler.NewThreadScheduler;
import com.yunzhi.net.utils.rsa.RSAUtils;

import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;

/**
 * @desc 整个接口文件
 * <p>
 * M层：业务逻辑的处理，实现了观察者中的方法，将其中请求的结果传递到抽象方法中，方便其他类的实现。（注意这里异常的传递和订阅关系的添加）
 */
public class AppApiService extends BaseApiService {
    private final ShopApi api;

    private AppApiService() {
        api = RetrofitFactory.getInstance().create(ShopApi.class);
    }


    private static class APIServiceHolder {
        private static final AppApiService INSTANCE = new AppApiService();
    }

    public static AppApiService getInstance() {
        return APIServiceHolder.INSTANCE;
    }

    private Map<String, Object> setDetail(Map<String, Object> map) {
        Map<String, Object> map1 = getPublicParams();
        if (map != null) {
            map1.putAll(map);
        }
        String detail = new Gson().toJson(map1);
        String da = RSAUtils.encryptByPublicKeyNew(detail);
        String strdetail = "";
        try {
            strdetail = URLEncoder.encode(da, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Map<String, Object> mapkey = new HashMap<>();
        mapkey.put("encrypt", strdetail);
        return mapkey;
    }

    /**
     * 示例接口
     */
    public void initApp(Map<String, Object> map, NetObserver<BaseResponse<Object>> t) {
        api.initApp(putParams(map)).compose(new NewThreadScheduler<>()).subscribe(t);
    }

    /**
     * 登录
     */
    public void initLogin(Map<String, Object> map, NetObserver<BaseResponse<UserInfoBean>> t) {
        api.initLogin(putParams(map)).compose(new NewThreadScheduler<>()).subscribe(t);
    }

//    /**
//     * 登录3
//     */
//    public void initLogin3(Map<String, Object> map, NetObserver<BaseResponse<UserInfoBean>> t) {
//
//        JSONObject json =new JSONObject(map);
//        RequestBody inf = new RequestBody() {
//            @javax.annotation.Nullable
//            @Override
//            public MediaType contentType() {
//                return null;
//            }
//
//            @Override
//            public void writeTo(BufferedSink sink) throws IOException {
//
//            }
//        };
//        RequestBody info = String.valueOf(json);
//        api.initLogin3(info).compose(new NewThreadScheduler<>()).subscribe(t);
//    }
    /**
     * 登录
     */
    public void initLogin3(Map<String, Object> map, NetObserver<BaseResponse<UserInfoBean>> t) {
        String info = new Gson().toJson(map);
        api.initLogin3(map).compose(new NewThreadScheduler<>()).subscribe(t);
    }


    /**
     * 健跑任务
     */
    public void runningTask(Map<String, Object> map, NetObserver<BaseResponse<Object>> t) {
        api.runningTask(putParams(map)).compose(new NewThreadScheduler<>()).subscribe(t);
    }

    /**
     * 开始当前健跑任务
     */
    public void runningTaskStart(Map<String, Object> map, NetObserver<BaseResponse<Object>> t) {
        api.runningTaskStart(putParams(map)).compose(new NewThreadScheduler<>()).subscribe(t);
    }

    /**
     * 结束当前健跑任务
     */
    public void runningTaskFinish(Map<String, Object> map, NetObserver<BaseResponse<Object>> t) {
        api.runningTaskFinish(putParams(map)).compose(new NewThreadScheduler<>()).subscribe(t);
    }

}
